package com.jingyanzi.marketing.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.GoodsMapper;
import com.jingyanzi.marketing.domain.GoodsDo;
import com.jingyanzi.marketing.entity.MaGoods;
import com.jingyanzi.marketing.entity.MaGoodsExample;
import com.jingyanzi.marketing.entity.MaGoodsExample.Criteria;
import com.jingyanzi.marketing.query.GoodsQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IGoodsService;
import com.jingyanzi.marketing.util.BeanUtilsExtends;

@Service("maGoodsService")
public class GoodsServiceImpl extends BaseServiceImpl implements IGoodsService {

	@Autowired
	private GoodsMapper goodsMapper;

	public ResultDO add(GoodsDo maGoodsDo) {
		MaGoods maGoods = new MaGoods();

		ResultSupport result = BeanUtilsExtends.copy(maGoods, maGoodsDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		maGoods.setCreatetime(Calendar.getInstance().getTime());
		maGoods.setIsDelete(1);
		int r = -1;

		try {
			r = goodsMapper.insert(maGoods);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加活动商品失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加活动商品失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, maGoods.getId());
		return result;
	}

	public ResultDO modifi(GoodsDo maGoodsDo) {
		MaGoods maGoods = new MaGoods();

		ResultSupport result = BeanUtilsExtends.copy(maGoods, maGoodsDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			r = goodsMapper.updateByPrimaryKeySelective(maGoods);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改活动商品失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改活动商品失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO remove(Integer id) {
		MaGoods maGoods = new MaGoods();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		maGoods.setId(id);
		maGoods.setIsDelete(-1);
		int r = -1;

		try {
			r = goodsMapper.updateByPrimaryKeySelective(maGoods);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除活动商品失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除活动商品失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO get(Integer id) {
		MaGoods maGoods = new MaGoods();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;

		try {
			maGoods = goodsMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取活动商品失败");
			result.setSuccess(false);
			return result;
		}

		if (maGoods == null) {
			result.setErrorCode(ResultDO.GOODS_GET_ERROR);
			result.setErrorMsg("不存在该活动商品");
			result.setSuccess(false);
			return result;
		}
		GoodsDo goodsDo = getMaGoodsDo(maGoods);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, goodsDo);
		return result;
	}

	public ResultDO query(GoodsQuery maGoodsQuery) {
		ResultSupport result = new ResultSupport();

		List<MaGoods> list = null;

		MaGoodsExample example = new MaGoodsExample();
		Criteria createCriteria = example.createCriteria();
		if (StringUtils.isNotBlank(maGoodsQuery.getName())) {
			createCriteria.andNameEqualTo(maGoodsQuery.getName());
		}

		if (maGoodsQuery.getType() != null) {
			createCriteria.andTypeEqualTo(maGoodsQuery.getType());
		}
		if (maGoodsQuery.getActivityId() != null) {
			createCriteria.andActivityIdEqualTo(maGoodsQuery.getActivityId());
		}
		createCriteria.andIsDeleteEqualTo(1);

		List<GoodsDo> maGoodsDos = null;

		if (maGoodsQuery.isPage()) {
			example.setLimitByPage(maGoodsQuery.getStart() + "," + maGoodsQuery.getPageRows());
			long count = goodsMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = goodsMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取活动商品失败");
			result.setSuccess(false);
			return result;
		}
		maGoodsDos = getMaGoodsDoList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, maGoodsDos);

		return result;

	}

}
